package com.ec50n9.server.auth

import cn.dev33.satoken.exception.NotLoginException
import cn.dev33.satoken.exception.NotPermissionException
import com.ec50n9.server.controllerAdvice.ExceptionAdvice
import com.ec50n9.server.module.common.AjaxResult
import com.ec50n9.server.module.common.AuthCode
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

@RestControllerAdvice
class AuthExceptionAdvice {

    val logger: Logger = LoggerFactory.getLogger(ExceptionAdvice::class.java)

    @ExceptionHandler
    fun notLogin(exception: NotLoginException): AjaxResult<Nothing> {
        logger.error(exception.stackTraceToString())
        return AjaxResult.fail(AuthCode.NOT_LOGIN, exception.message)
    }

    @ExceptionHandler
    fun notPermission(exception: NotPermissionException): AjaxResult<Nothing> {
        logger.error(exception.stackTraceToString())
        return AjaxResult.fail(AuthCode.NOT_PERMISSION, exception.message)
    }
}